package com.choosefine.springboot.devops.base.config;


import com.choosefine.springboot.devops.base.common.bean.CommonToolkitBean;
import com.choosefine.springboot.devops.base.common.bean.SimpleTemplateBean;
import com.choosefine.springboot.devops.base.moudle.event.*;
import com.choosefine.springboot.devops.base.service.BaseService;
import com.choosefine.springboot.devops.base.weixin.Event;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by 邱鹏 on 2016/11/10.
 */
@Configuration
public class BeanConfig {
    Logger logger = LoggerFactory.getLogger("趋恒科技：BeanConfig");

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
        setSerializer(template); //设置序列化工具
        template.afterPropertiesSet();
        return template;
    }

    private void setSerializer(StringRedisTemplate template) {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
    }

    @Bean
    public Properties yaml() {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        Properties resultProperties = null;
        try {
            logger.info("从<配置文件>中<>读取");
            //yaml.setResources(resolver.getResources("classpath:config/**/*.yml"));
            resultProperties = yaml.getObject();
        } catch (Exception e) {
            logger.error("无法读取 yaml 文件", e);
            resultProperties = null;
        }
        return resultProperties;
    }

    @Bean
    public CommonToolkitBean commonToolkitBean() {
        return new CommonToolkitBean();
    }

    @Bean
    public SimpleTemplateBean simpleTemplateBean() {
        return new SimpleTemplateBean();
    }

    @Bean
    public Map<String, EventCallback> eventCallbackMap() {
        Map<String, EventCallback> eventCallbackMap = new HashMap();
        eventCallbackMap.put("LOCATION", new LocationEvent());
        eventCallbackMap.put("subscribe",new SubscribeEvent());
        eventCallbackMap.put("unsubscribe",new UnsubscribeEvent());
        eventCallbackMap.put("CLICK",new ClickEvent());
        eventCallbackMap.put("default",new DefaultEvent());
        return eventCallbackMap;
    }

    @Bean
    public BaseService baseService(){
        return new BaseService();
    }

    @Bean
    public Event event(){return new Event();}

    // 跨域设置
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/activity/**")
                        .allowedMethods("*")
                        .allowedOrigins("*");
            }
        };
    }
}
